package sg.edu.nus.iss.cerberus.actions;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;

import sg.edu.nus.iss.cerberus.util.CerberusConstants;

public class JUnitCheck {

	private IJavaProject ijavaProject;
	
	public JUnitCheck(IJavaProject ijavaProject) {
		this.ijavaProject =ijavaProject ;
	}
			
	public boolean checkJUnitInBuildPath() throws JavaModelException{
		IClasspathEntry[] library = ijavaProject.readRawClasspath();
		for (IClasspathEntry cpEntry : library){
			String[] cp = cpEntry.getPath().segments();
				for (String str : cp){
					if (str.equals(CerberusConstants.JUNIT_PLUGIN_NAME)){
						return true;
					}
				}
		}
		return false;
	}
	
	public void addJUnitToBuildPath() throws JavaModelException{
		IClasspathEntry[] libraryList = ijavaProject.readRawClasspath();
		IClasspathEntry[] newLibraryList = new IClasspathEntry[libraryList.length+1];
		IClasspathEntry junitEntry = JavaCore.newContainerEntry(getJUnitPath());
		System.arraycopy(libraryList, 0, newLibraryList, 0, libraryList.length);
		newLibraryList[newLibraryList.length - 1] = junitEntry;
		ijavaProject.setRawClasspath(newLibraryList,new NullProgressMonitor());
	}
	
	private Path getJUnitPath(){
		Path junitPath = new Path(CerberusConstants.JUNIT_PATH);
		return junitPath;
	}
}